package com.ratta.supernote.note.plugincore;

import android.util.Log;

import androidx.annotation.Nullable;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.module.model.ReactModuleInfo;
import com.facebook.react.module.model.ReactModuleInfoProvider;
import com.facebook.react.TurboReactPackage;
import com.ratta.supernote.note.plugincore.core.PluginAppAPI;
import com.ratta.supernote.note.plugincore.modules.FileSelector;
import com.ratta.supernote.note.plugincore.modules.NoteTrailsModule;
import com.ratta.supernote.note.plugincore.modules.PluginModule;
import com.ratta.supernote.note.plugincore.modules.RNNoteAPIModule;
import com.ratta.supernote.note.plugincore.modules.RTNEvent;
import com.ratta.supernote.note.plugincore.modules.RTNFileModule;
import com.ratta.supernote.note.plugincore.modules.RTNUIUtils;

import java.util.HashMap;
import java.util.Map;

public class PluginPackage extends TurboReactPackage {
    private final String TAG = "PluginPackage";

    PluginAppAPI pluginApp;

    public PluginPackage(PluginAppAPI pluginApp) {
        this.pluginApp = pluginApp;
    }

    public PluginPackage() {
    }

    @Nullable
    @Override
    public NativeModule getModule(String name, ReactApplicationContext reactContext) {
        Log.i(TAG, "getModule test name:" + name);
        if (name.equals(RTNEvent.NAME)) {
            return new RTNEvent(reactContext, pluginApp);
        } else if (name.equals(FileSelector.NAME)) {
            return new FileSelector(reactContext);
        } else if (name.equals(RNNoteAPIModule.NAME)) {
            return new RNNoteAPIModule(reactContext, pluginApp);
        } else if (name.equals(RTNFileModule.NAME)) {
            return new RTNFileModule(reactContext);
        } else if (name.equals(RTNUIUtils.NAME)) {
            return new RTNUIUtils(reactContext);
        } else if (name.equals(PluginModule.NAME)) {
            return new PluginModule(reactContext, pluginApp);
        } else if (name.equals(NoteTrailsModule.NAME)) {
            return new NoteTrailsModule(reactContext);
        }
        return null;
    }

    @Override
    public ReactModuleInfoProvider getReactModuleInfoProvider() {
        return () -> {
            final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
            moduleInfos.put(
                    RTNEvent.NAME,
                    new ReactModuleInfo(
                            RTNEvent.NAME,
                            RTNEvent.NAME,
                            false, // canOverrideExistingModule
                            false, // needsEagerInit
                            false, // isCxxModule
                            true // isTurboModule
                    ));
            moduleInfos.put(
                    FileSelector.NAME,
                    new ReactModuleInfo(
                            FileSelector.NAME,
                            FileSelector.NAME,
                            false, // canOverrideExistingModule
                            false, // needsEagerInit
                            false, // isCxxModule
                            true // isTurboModule

                    ));
            moduleInfos.put(
                    RNNoteAPIModule.NAME,
                    new ReactModuleInfo(
                            RNNoteAPIModule.NAME,
                            RNNoteAPIModule.NAME,
                            false,
                            false,
                            false,
                            true
                    )
            );

            moduleInfos.put(
                    RTNUIUtils.NAME,
                    new ReactModuleInfo(
                            RTNUIUtils.NAME,
                            RTNUIUtils.NAME,
                            false,
                            false,
                            false,
                            true
                    )
            );

            moduleInfos.put(
                    RTNFileModule.NAME,
                    new ReactModuleInfo(
                            RTNFileModule.NAME,
                            RTNFileModule.NAME,
                            false,
                            false,
                            false,
                            true
                    )
            );

            moduleInfos.put(
                    PluginModule.NAME,
                    new ReactModuleInfo(
                            PluginModule.NAME,
                            PluginModule.NAME,
                            false,
                            false,
                            false,
                            true
                    )
            );
            moduleInfos.put(
                    NoteTrailsModule.NAME,
                    new ReactModuleInfo(
                            NoteTrailsModule.NAME,
                            NoteTrailsModule.NAME,
                            false,
                            false,
                            false,
                            true
                    )
            );
            return moduleInfos;
        };
    }
}